package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.connect.DBconnect;
import com.toolbean.*;

public class Register extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		  doPost(request,response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String driverClass = "com.mysql.jdbc.Driver";
		String user = "root";
        String pwd = "123456";
		String url = "jdbc:mysql://localhost:3306/shopdb?useUnicode=true&characterEncoding=gb2312";	
		Connection con=null;
		PreparedStatement pst = null;
		Statement st = null;
		ResultSet rs= null;
		response.setContentType("text/html;charset=gb2312");
		String path=request.getContextPath();
		PrintWriter out = response.getWriter();
		MyTools mytools=new MyTools();
		String username=mytools.toChinese(request.getParameter("Name"));
	     String password=request.getParameter("Password");
	     System.out.println(username);
		DBconnect dbcon = new DBconnect(driverClass,url,pwd,user);
		try {
			con = dbcon.getConnection();
			String sql="insert into user(username,userpassword)values(?,?)";
		     pst = con.prepareStatement(sql);
			 pst.setString(1,username);
			 pst.setString(2,password);
			 int a=pst.executeUpdate();
			 if(a==1){
				 out.println("ע��ɹ�,5��󷵻ص�¼ҳ��");
				 String chooseURL = "http://localhost:8080/homework/index.jsp";
				    response.setHeader("Refresh","5;URL="+chooseURL); 
			 }else{
				 out.println("ע��ʧ��");
				 String chooseURL = "http://localhost:8080/homework/zhuce.jsp";
				    response.setHeader("Refresh","5;URL="+chooseURL);
			 }
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
}
